﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;

namespace cerTOpfx
{
    public partial class mainForm : Form
    {

        private string certificateFileName;
        private string certificatePostfix;

        public mainForm()
        {
            InitializeComponent();
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            if (openCertOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePathTextBox.Text = openCertOpenFileDialog.FileName;
                certificateFileName = openCertOpenFileDialog.FileName;
                certificatePostfix = certificateFileName.Split('.').LastOrDefault();
            }
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            X509Certificate cert = new X509Certificate(certificateFileName);
            byte[] certData = cert.Export(X509ContentType.Pkcs12, "!cbiatwt2");

            certificateFileName = certificateFileName.Replace(certificatePostfix, "pfx");

            System.IO.File.WriteAllBytes(certificateFileName, certData);

            MessageBox.Show("Done");
        }
    }
}
